package com.syw.houmao.main.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.syw.houmao.R;
import com.syw.houmao.main.bean.FirstPagerCategory;
import com.syw.houmao.main.bean.FirstPagerCategoryInnerBean;
import com.syw.houmao.main.bean.FirstPagerCategoryInnerDataBean;
import com.syw.houmao.main.paly.view.PlayActivity;

/**
 * Created by Administrator on 2016/10/19.
 */

public class MainListAdapter extends BaseAdapter implements View.OnClickListener{


    private Context mContext;
    public MainListAdapter(Context context){
        mContext = context;
    }
    FirstPagerCategory mCategory;

   public void setCategory(FirstPagerCategory category){
        mCategory = category;
        notifyDataSetChanged();
    }
   public void swapCategory(FirstPagerCategory category){
        mCategory =category;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCategory!=null?mCategory.getData().size():0;
    }

    @Override
    public Object getItem(int position) {
        return mCategory!=null?mCategory.getData().get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView ==null){
            convertView = LayoutInflater.from(parent.getContext().getApplicationContext()).inflate(R.layout.main_list_item,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        FirstPagerCategoryInnerBean categoryInnerBean = mCategory.getData().get(position);
        holder.vMore.setOnClickListener(this);

        MainGridAdapter adapter = new MainGridAdapter();
        holder.vCname.setText(categoryInnerBean.cname);
        Picasso.with(mContext).load(categoryInnerBean.getIcon()).into(holder.vIcon);
        FirstPagerCategoryInnerDataBean bean = categoryInnerBean.getData().get(position);
        adapter.setData(categoryInnerBean);
        holder.mGrid.setAdapter(adapter);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        mContext.startActivity(new Intent(mContext, PlayActivity.class));
    }

    static class ViewHolder{
        ImageView vIcon;
        TextView vCname,vMore;
        GridView mGrid;
        public ViewHolder(View view){
            vIcon = (ImageView) view.findViewById(R.id.main_icon);
            vMore = (TextView) view.findViewById(R.id.main_more);
            vCname = (TextView) view.findViewById(R.id.main_cname);
            mGrid = (GridView) view.findViewById(R.id.main_grid);
        }
    }
}
